<template>
  <!--新增或者更新-->
  <div>
    <el-dialog
        title="提示"
        :close-on-click-modal=false
        :visible.sync="dialogVisible"
        width="600px"
        :before-close="handleClose">

      <el-form :model="editForm" :rules="editFormRules" ref="editForm" label-width="100px" class="demo-editForm">

        <el-form-item label="角色名称" prop="name" label-width="100px">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="唯一编码" prop="code" label-width="100px">
          <el-input v-model="editForm.code" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="描述" prop="remark" label-width="100px">
          <el-input v-model="editForm.remark" autocomplete="off"></el-input>
        </el-form-item>


        <el-form-item label="状态" prop="statu" label-width="100px">
          <el-radio-group v-model="editForm.statu">
            <el-radio :label=0>禁用</el-radio>
            <el-radio :label=1>正常</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitForm('editForm')">提交</el-button>
          <el-button @click="resetForm('editForm')">重置</el-button>
        </el-form-item>
      </el-form>

    </el-dialog>

  </div>
</template>

<script>
import {save, getPermissionListById} from "@/api/sys/role";

export default {
  name: "RoleAddRoleInfo",
  data() {
    return {
      dialogVisible: false,
      editForm: {},
      editFormRules: {
        name: [
          {required: true, message: '请输入角色名称', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入唯一编码', trigger: 'blur'}
        ],
        statu: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ]
      },
    }
  },
  methods: {
    //关闭之前处理
    handleClose() {
      this.dialogVisible = false
      this.resetForm('editForm')
    },
    //提交表单
    submitForm() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          this.loading = true
          const data = this.editForm
          save(data).then(res => {
            this.$emit("handle", res)
            this.loading = false
            this.handleClose()
            this.$message({
              showClose: true,
              message: '恭喜你，操作成功',
              type: 'success',
            });
          })
        }
      })
    },
    //重置表单
    resetForm() {
    },
    //  打开对话框 父组件调用
    openDialog() {
      this.dialogVisible = true
    },
    setEditForm(id) {
      getPermissionListById(id).then(res => {
        this.editForm = res.data
      })
    }
  }
}
</script>

<style scoped>

</style>
